$NG.AllReady(function (page, { useAction, useBeforeClick, useValuesChange, useClick, useUpdateRow, subscribe, useUpdateRows }) {
    const mstform = $NG.getCmpApi("p_form_ydczjhwcb_m")
    const dgrid1 = $NG.getCmpApi("p_form_ydczjhwcb_d1")
    const getHeadValue = (str) => mstform.getItem(str).getValue()

    let config = {
        currentYeatAmt:0
    }

    if (page.oType == 'add') {
        getToMonth()
        getOneMonth()
        mstform.getItem("u_nd").setValue({
            label: new Date().getFullYear(),
            value: new Date().getFullYear()
        })
    }

    useValuesChange(async () => {
        await getOneMonth()
        getToMonth()
        getCntCbInfoByPhid()
        monthAmtTotal()
        yearAmt()
    }, "p_form_ydczjhwcb_m.u_htmc")

    useValuesChange(async () => {
        await getOneMonth()
        getToMonth()
    }, "p_form_ydczjhwcb_m.u_yd")

    useUpdateRows(()=>{
        const rows = dgrid1.getRows()
        rows.forEach(el=>{
            el.u_njh = config.currentYeatAmt
            el.u_zncljwc = (Number(config.monthAmt) || 0) + (Number(el.u_ysjwc) || 0)
        })
        dgrid1.refreshView()
    },"p_form_ydczjhwcb_d1")

    useUpdateRow(({ args }) => {
        console.log(`args`, args[0]);
        const item = args[0]
        if (getHeadValue("u_yd") != '6870000000000553' && (Number(item.u_njh) || 0) != config.currentYeatAmt) {
            $NG.message("一月份外，年计划不得修改!")
            item.u_njh = config.currentYeatAmt
        }
        dgrid1.refreshView()
    })
    useBeforeClick(() => {
        const u_yd = mstform.getItem("u_yd").getValue()
        if (!u_yd) {
            updateRowslock = true
            $NG.alert("请先填写表头月度字段！")
            return false
        }
    }, "addrow")

// 获得合同信息
    function getCntCbInfoByPhid() {
        const phid = getHeadValue("u_htmc")
        if (!phid) return
        $NG.execServer("getCntCbInfoByPhid", { phid }, (res) => {
            if (res.count == 0) return
            const datas = JSON.parse(res.data)[0].extendObjects
            const arr = [
                {
                    key:"u_xmmc",
                    value: {
                        label: datas.phid_pc_exname,
                        value: datas.phid_pc
                    }
                }
            ]
            setHeadValue(arr)
        })
    }

    // 获取年度产值
    function yearAmt(){
        const phid = getHeadValue("u_htmc")
        if(!phid) return
        $NG.execServer("SELECT",{sql:`select u_bnczjh from p_form_ndczjh_m where u_htmc = '${phid}' and app_status = 1`},(res)=>{
            if(res.count == 0) return 
            const datas = JSON.parse(res.data)[0].extendObjects
            config.currentYeatAmt = datas.u_bnczjh
        })
    }

    // 获取前几个月的金额
    function monthAmtTotal(){
        const phid = getHeadValue("u_htmc")
        if(!phid) return 
        $NG.execServer("SELECT",{sql:`select sum(d.u_ysjwc) as amt from p_form_ydczjhwcb_m m left join p_form_ydczjhwcb_d1 d on m.phid = d.pphid where m.u_htmc='${phid}' and m.app_status = 1`},(res)=>{
            if(res.count == 0) return 
            const datas = JSON.parse(res.data)[0].extendObjects
            config.monthAmt = datas.amt
        })
    }

    // 拉取上个月的金额
    async function getToMonth() {
        let phid = mstform.getItem("u_yd").getValue()
        let cnt = mstform.getItem("u_htmc").getValue()
        if (!phid || !cnt) return
        phid = (Number(phid) || 0)
        if (phid != 6870000000000553) {
            phid--
        } else {
            dgrid1.clearRows()
            return
        }
        await $NG.execServer("SELECT", { sql: `select d.u_xyczjh from p_form_ydczjhwcb_m m left join p_form_ydczjhwcb_d1 d on d.pphid= m.phid where m.u_yd = '${phid}' and m.u_htmc = '${cnt}'` }, (res) => {
            if (res.count == 0) return
            const datas = JSON.parse(res.data)[0].extendObjects
            dgrid1.clearRows().then(() => {
                dgrid1.addRows({
                    u_yjh: datas.u_xyczjh,
                })
            })
        })
    }

    // 拉取一月的年计划
    async function getOneMonth() {
        let phid = 6870000000000553
        let cnt = mstform.getItem("u_htmc").getValue()
        if (!phid || !cnt) return
        await $NG.execServer("SELECT", { sql: `select d.u_njh from p_form_ydczjhwcb_m m left join p_form_ydczjhwcb_d1 d on d.pphid= m.phid where m.u_yd = '${phid}' and m.u_htmc = '${cnt}'` }, (res) => {
            if (res.count == 0) return
            const datas = JSON.parse(res.data)[0].extendObjects
            dgrid1.clearRows().then(() => {
                window._cath_one_month_njh = datas.u_njh
            })
        })
    }
 // 赋值
    function setHeadValue(datas) {
        datas.forEach(item => {
            try {
                mstform.getItem(item.key).setValue(item.value)
            } catch (err) {
                console.log(`err`, err, item);
            }
        })
    }
})